package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbSaveStudent;
import com.beetlestech.nfe.main.biz.com.dto.StudentaddlessonInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.StudenteditDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant.CATAGORY_P_MESSAGE_STATE;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.MstfileDTO;
import com.beetlestech.nfe.main.db.tbl.MststudentDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.tbl.StudentaddlessonDTO;
import com.beetlestech.nfe.main.db.tbl.UsermessageDTO;


public class HbSaveStudentImpl extends AbstractDBLogic implements HbSaveStudent, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "学生信息表";
	//修改学生信息
	@Override
	public void updateStudentBaseInfo(
			Integer stuid,
			String username,
			String	 password,
			String name,
			Integer sex,
			Integer birthdayyear,
			String email,
			String phone,
			Integer areacity,
			String school,
			String professional){
		//dbDTO
		MststudentDTO student = new MststudentDTO();
		
		student.setusrid(stuid);
		student.setname(name);
		student.setsex(sex);
		student.setbirthdayyear(birthdayyear);
		student.setareacity(areacity);
		student.setschool(school);
		student.setprofessional(professional);
		//对DB执行操作
		Integer uid=student.getusrid();
		updateDAO.updateOne(MststudentDTO.UPDATE, student, M10190WS, SH_INFO);
		MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusrid(uid);
		userDTO.setusername(username);
		userDTO.setemail(email);
		userDTO.setphone(phone);
		userDTO.settype(0);
		userDTO.setstate(1);
		userDTO.setfirstloginflg(0);
		updateDAO.updateOne(MstusrDTO.UPDATE, userDTO, M10190WS, SH_INFO);
	}
	//根据学生ID 获取信息
	@Override
	public StudenteditDTO GetStudentBySid(Integer stuid){
		//自定义DTO
		StudenteditDTO stuDTO=new StudenteditDTO();
		//DBStudentDTO
		MststudentDTO student = new MststudentDTO();
		student.setusrid(stuid);
		student = queryDAO.queryForObject(MststudentDTO.EQUAL_SEARCH, student);
		//DBuserDTO
		MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusrid(stuid);
		userDTO = queryDAO.queryForObject(MstusrDTO.EQUAL_SEARCH, userDTO);
		
		//给自定义DTO赋值
				stuDTO.setUsername(userDTO.getusername());
				stuDTO.setPhone(userDTO.getphone());
				stuDTO.setEmail(userDTO.getemail());
				stuDTO.setName(student.getname());
				stuDTO.setSex(student.getsex());
				stuDTO.setSchool(student.getschool());
				stuDTO.setProfessional(student.getprofessional());
				stuDTO.setBirthdayyear(student.getbirthdayyear());
				stuDTO.setAreacity(student.getareacity());
				stuDTO.setPassword(userDTO.getpassword());
		
		return stuDTO;
	}
	//新增学生信息
	@Override
	public void SaveStudentBaseInfo(
			String username,
			String	 password,
			String name,
			Integer sex,
			Integer birthdayyear,
			String email,
			String phone,
			Integer areacity,
			String school,
			String professional
	){
			//向USERDTO里插入的数据

					checkStudentInfo(username,email,phone);
					MstusrDTO userDTO = new MstusrDTO();
					
					userDTO.setusername(username);
					userDTO.setpassword(password);
					userDTO.setemail(email);
					userDTO.setphone(phone);
					//学生TYPE
					userDTO.settype(0);
					userDTO.setstate(1);
					userDTO.setfirstloginflg(0);
					//对DB执行操作
					updateDAO.updateOne(MstusrDTO.INSERT, userDTO, M10190WS, SH_INFO);
		  //向学生表里插入数据
					MststudentDTO student = new MststudentDTO();
					student.setusrid(userDTO.getusrid());
					student.setname(name);
					student.setsex(sex);
					student.setbirthdayyear(birthdayyear);
					student.setareacity(areacity);
					student.setschool(school);
					student.setprofessional(professional);
					
					//对DB执行操作
					updateDAO.updateOne(MststudentDTO.INSERT, student, M10190WS, SH_INFO);
	
		
	}
	//check 用户名,邮箱，电话 唯一性
	private void checkStudentInfo(String username,String email,String phone) {
		MstusrDTO mstusrDTO =new MstusrDTO(); 
		mstusrDTO.setusername(username);
		MstusrDTO mstusrDTOe =new MstusrDTO(); 
		mstusrDTOe.setemail(email);
		MstusrDTO mstusrDTOp =new MstusrDTO(); 
		mstusrDTOp.setphone(phone);
		
		if(queryDAO.count(MstusrDTO.COUNT, mstusrDTO) > 0){
				createApplicationException(M10070WC);
		} 
		if(phone!=null){
			if(queryDAO.count(MstusrDTO.COUNT, mstusrDTOp) > 0){
				createApplicationException(M10050WC);
			}
		}
		if(email!=null){
			if(queryDAO.count(MstusrDTO.COUNT, mstusrDTOe) > 0){
				createApplicationException(M10060WC);
			}
		}
		}
	//新增补课
	@Override
	public void saveStudentAddlesson(
									Integer stuid,
									String lessonName,
									Integer fileId){
		
		StudentaddlessonDTO stuAddDto=new StudentaddlessonDTO();
			stuAddDto.setstudentid(stuid);
			stuAddDto.setlessonname(lessonName);
			stuAddDto.setfileid(fileId);
			updateDAO.updateOne(StudentaddlessonDTO.INSERT, stuAddDto, M10190WS, SH_INFO);
			//send email
			UsermessageDTO userMessage = new UsermessageDTO();
			String msg="A课堂为您添加新的补课【"+lessonName+"】，请在【我的补课】内查看！";
			userMessage.setTitel(msg);
			userMessage.setMessage(msg);
			userMessage.setSenderId(SERVER_ID);
			userMessage.setReceiveId(stuid);
			userMessage.setSendDT(getCurrentDate());
			userMessage.setSendTime(getCurrentTime());
			userMessage.setReadflg(Integer.parseInt(CATAGORY_P_MESSAGE_STATE.UNREAD.toString()));
			updateDAO.update(UsermessageDTO.INSERT, userMessage);
	}
	//补课一览
	@Override
	public StudentaddlessonInfoDTO[] getStudentAddlessonListBySid(Integer stuid){
		StudentaddlessonDTO stuDTO=new StudentaddlessonDTO();
		stuDTO.setstudentid(stuid);
		List<StudentaddlessonDTO> list=queryDAO.queryForObjectList(StudentaddlessonDTO.EQUAL_SEARCH, stuDTO);
		List<StudentaddlessonInfoDTO> listinfo=new ArrayList<StudentaddlessonInfoDTO>();
		for(int i=0;i<list.size();i++){
			StudentaddlessonInfoDTO stuInfoDTO=new StudentaddlessonInfoDTO();
			stuInfoDTO.setaddlessonid(list.get(i).getaddlessonid());
			stuInfoDTO.setstudentid(list.get(i).getstudentid());
			stuInfoDTO.setlessonname(list.get(i).getlessonname());
			stuInfoDTO.setfileid(list.get(i).getfileid());
			
			MstfileDTO ms=new MstfileDTO();
			ms.setfileid(list.get(i).getfileid());
			ms=queryDAO.queryForObject(MstfileDTO.EQUAL_SEARCH, ms);
			
			stuInfoDTO.setFilename(ms.getfilename());
			stuInfoDTO.setlastdate(list.get(i).getlastdate());
			listinfo.add(stuInfoDTO);
		}
			return listinfo.toArray(new StudentaddlessonInfoDTO[listinfo.size()]);
	}
	
	//删除补课
	@Override
	public void deleteLesson (Integer addlessonId){
		StudentaddlessonDTO stuDTO=new StudentaddlessonDTO();
		stuDTO.setaddlessonid(addlessonId);
		stuDTO=queryDAO.queryForObject(StudentaddlessonDTO.EQUAL_SEARCH, stuDTO);
		updateDAO.update(StudentaddlessonDTO.DELETE, stuDTO);
	}
	
	
}
